package com.qkbb.admin.kuibu.qkbb.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.PolylineOptions;
import com.amap.api.services.road.Road;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.adapter.LookAdapter;
import com.qkbb.admin.kuibu.qkbb.JavaBean.Point;
import com.qkbb.admin.kuibu.qkbb.fragment.LookWay_1;
import com.qkbb.admin.kuibu.qkbb.fragment.LookWay_2;
import com.qkbb.admin.kuibu.qkbb.fragment.LookWay_3;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.CustomProgressDialog;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LookThisWay extends FragmentActivity {
    ViewPager viewPager;
    ArrayList<Fragment> fragmentList;
    private TitleBarView titleBarView;
    private MapView mapView;
    private AMap aMap;
    private int roadid;
    private String roadname;
    private ArrayList<Point> list;
    private TextView titletext;
    private CustomProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_this_way);
        mapView = (MapView) findViewById(R.id.lookthisway_bap);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        aMap = mapView.getMap();
        Intent intent=getIntent();
        roadid=intent.getIntExtra("roadid", 103);
        roadname=intent.getStringExtra("roadname");
        Log.e("roadname",roadname);
        viewPager= (ViewPager) findViewById(R.id.lookthisway_viewpager);
        titletext= (TextView) findViewById(R.id.lookthisway_title);
        progressDialog=CustomProgressDialog.createDialog(this);
        progressDialog.show();
        titletext.setText(roadname);
        titleBarView= (TitleBarView) findViewById(R.id.lookthisway_titlebar);
        titleBarView.setColor(R.color.touming);
        titleBarView.setLeftButton(R.mipmap.people_50_03);
        titleBarView.setRightButton(R.mipmap.search_50_03);
        titleBarView.setCenterTexiView("跬 步");

        LookWay_1 lookWay_1=new LookWay_1();
        LookWay_2 lookWay_2=new LookWay_2();
        LookWay_3 lookWay_3=new LookWay_3();
        fragmentList=new ArrayList<Fragment>();
        fragmentList.add(lookWay_1);
        fragmentList.add(lookWay_2);
        fragmentList.add(lookWay_3);
        LookAdapter lookAdapter=new LookAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(lookAdapter);
        getPoint(this);
    }
    public List<Point> getPoint(Context context){

        String user_token= SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        list=new ArrayList<Point>();
        RequestQueue queue= Volley.newRequestQueue(context);
        StringRequest stringRequest=new StringRequest(Url.GETPOINT1 + user_token + Url.GETPOINT2 + roadid, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("S", s);
                try {
                    JSONObject jsonObject =new JSONObject(s);
                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                    JSONArray jsonArray=jsonObject1.getJSONArray("Coords");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject2=jsonArray.getJSONObject(i);
                        Point point=new Point();
                        point.setDescription(jsonObject2.getString("description"));
                        point.setIdxno(jsonObject2.getInt("idxno"));
                        point.setLatitude(jsonObject2.getDouble("latitude"));
                        point.setLongitude(jsonObject2.getDouble("longitude"));
                        point.setRoadid(jsonObject2.getInt("roadid"));
                        point.setRoadlength(jsonObject2.getInt("roadlength"));
                        point.setRoadsteplength(jsonObject2.getInt("roadsteplength"));
                        point.setSteppercent(jsonObject2.getInt("roadsteplength"));
                        list.add(point);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                LatLng latLng[]=new LatLng[list.size()];
                for(int i=0;i<list.size();i++){
                    latLng[i]=new LatLng(list.get(i).getLatitude(),list.get(i).getLongitude());
                }
                aMap.addPolyline(new PolylineOptions().add(latLng).color(Color.YELLOW).width(10)).setVisible(true);
                aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.xiaolian)).position(latLng[0]).anchor(0.5f,0.5f));
                aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.zhongdian)).position(latLng[latLng.length-1]).anchor(0.5f,0.5f));
                CameraUpdate update= CameraUpdateFactory.newLatLngZoom(latLng[0], 14);
                aMap.moveCamera(update);
                progressDialog.cancel();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("volleyError", "" + volleyError);

            }
        });
        queue.add(stringRequest);
        return list;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
